package com.hust.edu.cn;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;


import java.util.ArrayList;
import java.util.Scanner;

public class MybatisGenerator {

        /**
         * <p>
         * 读取控制台内容
         * </p>
         */
        public static String scanner(String tip) {
            Scanner scanner = new Scanner(System.in);
            StringBuilder help = new StringBuilder();
            help.append("请输入" + tip + "：");
            System.out.println(help.toString());
            if (scanner.hasNext()) {
                String ipt = scanner.next();
                if (StringUtils.isNotBlank(ipt)) {
                    return ipt;
                }
            }
            throw new MybatisPlusException("请输入正确的" + tip + "！");
        }

        public static void main(String[] args) {
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();

            // 全局配置
            GlobalConfig gc = new GlobalConfig();
            String projectPath = System.getProperty("user.dir");
            gc.setOutputDir(projectPath + "/src/main/java");
            gc.setAuthor("Yu Jian")
                    .setActiveRecord(true); // 作者
            gc.setOpen(false)
                    .setIdType(IdType.AUTO);
            gc.setFileOverride(true)
                    .setBaseResultMap(true)
                    .setBaseColumnList(true);// 文件覆盖
            gc.setServiceName("%sService"); // 去掉Service的I前缀
            gc.setSwagger2(true); // 实体属性 Swagger2 注解
            gc.setDateType(DateType.ONLY_DATE);
            mpg.setGlobalConfig(gc);


            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setDbType(DbType.MYSQL);
            dsc.setUrl("jdbc:mysql://localhost:3306/blog?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
            dsc.setDriverName("com.mysql.cj.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("Xixi1118");
            mpg.setDataSource(dsc);

            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setModuleName(null);
            pc.setParent("com.hust.edu.cn");
            pc.setEntity("entity");
            pc.setController("controller");
            pc.setMapper("mapper");
            pc.setService("service");
            mpg.setPackageInfo(pc);


            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setCapitalMode(true)
                    .setInclude("user");
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
            strategy.setEntityLombokModel(true);
            strategy.setRestControllerStyle(true);
            strategy.setTablePrefix(pc.getModuleName() + "_");

            // 设置逻辑删除
            strategy.setLogicDeleteFieldName("deleted");
            // 自动填充配置
            TableFill gmt_Create = new TableFill("gmt_Create", FieldFill.INSERT);
            TableFill gmt_Modified = new TableFill("gmt_Modified", FieldFill.INSERT);
            ArrayList<TableFill> tableFills = new ArrayList<>();
            tableFills.add(gmt_Create);
            tableFills.add(gmt_Modified);
            strategy.setTableFillList(tableFills);

            // 乐观锁
            strategy.setVersionFieldName("version");
            strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
            strategy.setControllerMappingHyphenStyle(true);
            mpg.setStrategy(strategy);

            mpg.execute();

        }
}
